package cn.fl.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.system.condition.SysScoreIdxQueryCondition;
import cn.fl.system.dto.SysScoreIdxDTO;
import cn.fl.system.service.ISysScoreApiService;
import cn.fl.system.service.ISysScoreIdxService;
import cn.fl.system.transform.SysScoreIdxOptVOTransform;
import cn.fl.system.transform.SysScoreIdxVOTransform;
import cn.fl.system.vo.SysScoreIdxOptVO;
import cn.fl.system.vo.SysScoreIdxVO;

/**
 * @Description: 评分模板指标表 客户端服务层接口实现
 */
@Service
public class SysScoreIdxServiceImpl implements ISysScoreIdxService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysScoreIdxServiceImpl.class);

	/**
	 * 系统评分模版 API服务
	 */
	@Autowired
	private ISysScoreApiService sysScoreApiService;
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Override
	public SysScoreIdxVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		//TODO 业务逻辑
		SysScoreIdxDTO dto = this.sysScoreApiService.searchSysScoreIdxById(id);
		return SysScoreIdxVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysScoreIdxVO entity) throws BaseException{
		if(null == entity)return null;
		
		//TODO 业务逻辑
	//	return this.sysScoreApiService.addSysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
		
		Long id = null;

		SysScoreIdxQueryCondition qc = new SysScoreIdxQueryCondition();
		qc.setTmplCd(entity.getTmplCd());
		
		if(null!=entity){
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			id = this.sysScoreApiService.addSysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
			if(CollectionUtils.isNotEmpty(entity.getSysScoreIdxOptVOList())){
				for (SysScoreIdxOptVO sysScoreIdxOpt : entity.getSysScoreIdxOptVOList()) {
					sysScoreIdxOpt.setTmplIdxId(id);

					this.sysScoreApiService.addSysScoreIdxOpt(SysScoreIdxOptVOTransform.toDTO(sysScoreIdxOpt));//.insert(sysScoreIdxOpt);
				}
			}
			
			
		}
		
		return id;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.sysScoreIdxApiService.removeById(id);
		//TODO 业务逻辑
		return this.sysScoreApiService.removeSysScoreIdx(id.toString());
	}
	
	@Override
	public boolean removeByTmplId(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.sysScoreIdxApiService.removeById(id);
		//TODO 业务逻辑
		return this.sysScoreApiService.removeSysScoreIdxs(id.toString());
	}

	@Override
	public boolean modify(SysScoreIdxVO entity) throws BaseException{
		if(null == entity)return false;
		//return this.sysScoreIdxApiService.modify(SysScoreIdxVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return this.sysScoreApiService.modifySysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.sysScoreIdxApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}
	
	@Override
	public List<SysScoreIdxVO> searchIdxList(SysScoreIdxQueryCondition condition) throws BaseException{
		if(null == condition)condition = new SysScoreIdxQueryCondition();
		//return this.sysScoreIdxApiService.searchListPage(page);
		//TODO 业务逻辑
		return SysScoreIdxVOTransform.toVOList(this.sysScoreApiService.searchSysScoreIdxList(condition)) ;
	}

	@Override
	public Long addidxAndOpt(SysScoreIdxVO entity) throws BaseException {
		// TODO Auto-generated method stub
        if(null == entity)return null;
		
		//TODO 业务逻辑
	//	return this.sysScoreApiService.addSysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
		
		Long id = null;

		SysScoreIdxQueryCondition qc = new SysScoreIdxQueryCondition();
		qc.setTmplCd(entity.getTmplCd());
		
		if(null!=entity){
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			id = this.sysScoreApiService.addSysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
			if(CollectionUtils.isNotEmpty(entity.getSysScoreIdxOptVOList())){
				this.sysScoreApiService.removeSysScoreIdxOptByIdxId(id);
				for (SysScoreIdxOptVO sysScoreIdxOpt : entity.getSysScoreIdxOptVOList()) {
					sysScoreIdxOpt.setTmplIdxId(id);
					Long optid=sysScoreIdxOpt.getId();
					
		             //if(optid!=null){
		           //     	this.sysScoreApiService.modifySysScoreIdxOpt(SysScoreIdxOptVOTransform.toDTO(sysScoreIdxOpt));//.insert(sysScoreIdxOpt);	
		           //  }else{
		            	 this.sysScoreApiService.addSysScoreIdxOpt(SysScoreIdxOptVOTransform.toDTO(sysScoreIdxOpt));
		           //  }
				}
			}
			
			
		}
		
		return id;
	}

	@Override
	public boolean modifyidxAndOpt(SysScoreIdxVO entity) throws BaseException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
        if(null == entity)return false;
		
		//TODO 业务逻辑
	//	return this.sysScoreAentitypiService.addSysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
		
		Long id =entity.getId() ;

		SysScoreIdxQueryCondition qc = new SysScoreIdxQueryCondition();
		qc.setTmplCd(entity.getTmplCd());
		
		if(null!=entity){
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			 this.sysScoreApiService.modifySysScoreIdx(SysScoreIdxVOTransform.toDTO(entity));
			if(CollectionUtils.isNotEmpty(entity.getSysScoreIdxOptVOList())){
				this.sysScoreApiService.removeSysScoreIdxOptByIdxId(id);
				for (SysScoreIdxOptVO sysScoreIdxOpt : entity.getSysScoreIdxOptVOList()) {
					sysScoreIdxOpt.setTmplIdxId(id);
					Long optid=sysScoreIdxOpt.getId();
				
            // if(optid!=null){
            //    	this.sysScoreApiService.modifySysScoreIdxOpt(SysScoreIdxOptVOTransform.toDTO(sysScoreIdxOpt));//.insert(sysScoreIdxOpt);	
            // }else{
            	 this.sysScoreApiService.addSysScoreIdxOpt(SysScoreIdxOptVOTransform.toDTO(sysScoreIdxOpt));
           //  }
				}
			}
			
			
		}
		
		return true;
	}


}
